<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2018/8/24
 * Time: 15:59
 */
namespace app\withdraw;

use app\common\Common;
use app\data\WithdrawData;
use think\Db;

class ShowWithdraw extends Base
{
    /**
     * 排序方法
     * @var array
     */
    private $sortType = ['desc', 'asc'];

    /**1
     * 商户排序字段
     * @var array
     */
    private $withdrawSort = ['create_time'];


    /**
     * 提现申请显示(大后台)
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @param int $status
     * @param $order_num
     * @param $user_id
     * @param $type_status
     * @return \think\response\Json
     */
    public function withdrawShow($page = 1, $pre = 15, array $sort = ['create_time' => 'desc'],
                                 $status = 0, $order_num = 0, $user_id = 0, $type_status = 1)
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->withdrawSort, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $time = time();
        $time = strtotime(date('Y-m-d', $time - 24 * 60 * 60 * 7));//7天前的
        switch ($status) {
            case 0://所有的
                $where = '';
                break;
            case 1://可打款的
                $where = "a.create_time <= {$time} and a.review_status = 0";//7天前的可以打款
                break;
            case 2://不能打款的
                $where = "a.create_time > {$time} and a.review_status = 0";//7天后的不能打款
                break;
            case 3://驳回的
                $where = " a.review_status = 2";
                break;
            case 4://打款完成的
                $where = " a.review_status = 1";
                break;
            default:
                return json(['code' => $this->f_code, 'msg' => '状态错误']);
        }
        $withdraw = new WithdrawData();
        if ($order_num !== 0) {
            $where = "a.order_num='{$order_num}'";
        }
        if ($user_id != 0) {//前端显示
            if ($type_status != 1 && $type_status != 2) {
                return json(['code' => $this->f_code, 'msg' => '非法操作']);
            }
            if($where==''){
                $where = "a.user_id={$user_id} and a.review_type={$type_status}";
            }else{
                $where = $where . " and a   .user_id={$user_id} and a.review_type={$type_status}";
            }

        }
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $withdraw->withdrawShow($page, $pre, $sort, $where),
                'total' => $withdraw->withdrawTotal($where)
            ]
        ]);
    }

    /**
     * 显示以提现金额
     * @param $status
     * @param $id
     * @return \think\response\Json
     */
    public function showWithdrawSuccessMoney($status, $id)
    {
        if (Common::checkTableFields('drh_user_info', 'id', $id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '用户不存在']);
        }
        if ($status != 1 && $status != 2) {
            return json(['code' => $this->f_code, 'msg' => '操作不合法']);
        }
        $withdraw = new WithdrawData();
        $res = $withdraw->showWithdrawSuccessMoney($status, $id);
        if ($status == 1) {
            $res['withdraw_money'] = $res['shop_money'];
        } else {
            $res['withdraw_money'] = $res['user_money'];
        }
        unset($res['shop_money']);
        unset($res['user_money']);
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => $res
        ]);
    }
}